<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  {include="user/style"}
  <title>钱包与积分 - 个人中心</title>
</head>
<body>

{include="user/top"}

<div class="login-page page">
  <div class="container clearfix">
    {include="user/left_nav"}
    <div class="user-right">
      <div class="user-tips">
        <p><i class="iconfont iconxiaoxi3"></i> 欢迎注册本站会员，注册会员后您将享受专属会员服务!包括但不限于专属文章浏览权限，会员投稿权限，在线购物权限，下载会员可见附件等实用功能，欢迎注册体验！</p>
      </div>
      <div class="user-content">
        <h2>钱包与积分</h2>
        <form action="" method="POST" onsubmit="return checkform()" id="myupload" class="user-form">
          <input name="allmoney" id="allmoney" type="hidden" value="0" />
          <input name="paytype" id="paytype" type="hidden" value="1" />
          <div class="form-control">
            <label for="">交易类型：</label>
            <select id="buytarget" onchange="count_money()" name="buytarget" >
            <option value="1">钱包（兑换率:人民币 : 金币 = 1 : {$webconf['money_exchange']}）</option>
            <option value="2">积分（兑换率:人民币 : 积分 = 1 : {$webconf['jifen_exchange']}）</option>
            </select>
          </div>
          <div class="form-control">
            <label for="">购买数量：</label>
            <input type="number" value="0" name="money" onchange="count_money()" id="money" placeholder="请输入购买数量">
          </div>
          <div class="form-control">
            <label for="">支付金额：</label>
            <label id="paymoney" for="" style="color:#f00;font-weight: 600;">0</label>
            <label for="">元</label>
          </div>
          <div class="form-control">
            <label for="submit">支付方式：</label>
            {if($webconf['isopenzfb'])}
            <a class="zfbpay" href="javascript:;" onclick="choosepay('zfb')" style="filter: grayscale(0%);"><img style="width:100px;" src="{$common}user/images/zfb.png"></a>
            {/if}
            {if($webconf['isopenweixin'])}
            <a class="wxpay" href="javascript:;" onclick="choosepay('wx')" style="filter: grayscale(100%);"><img style="width:100px;" src="{$common}user/images/wx.png"></a>
            {/if}
          </div>
          <div class="form-control">
            <label for="submit"></label>
            <input type="submit" name="submit" value="提交">
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

{include="user/footer"}
<script type="text/javascript">
function choosepay(pt){
    if(pt=='zfb'){
        $(".zfbpay").attr('style',"filter: grayscale(0%);");
        $(".wxpay").attr('style',"filter: grayscale(100%);");
        $("#paytype").val(1);
    }else{
        $("#paytype").val(2);
        $(".zfbpay").attr('style',"filter: grayscale(100%);");
        $(".wxpay").attr('style',"filter: grayscale(0%);");
    }
}
  var floatObj = function () {
  /*
   * 判断obj是否为一个整数
   */
  function isInteger(obj) {
      return Math.floor(obj) === obj
  }

  /*
   * 将一个浮点数转成整数，返回整数和倍数。如 3.14 >> 314，倍数是 100
   * @param floatNum {number} 小数
   * @return {object}
   *   {times:100, num: 314}
   */
  function toInteger(floatNum) {
      var ret = {times: 1, num: 0};
      if (isInteger(floatNum)) {
          ret.num = floatNum;
          return ret
      }
      var strfi = floatNum + '';
      var dotPos = strfi.indexOf('.');
      var len = strfi.substr(dotPos + 1).length;
      var times = Math.pow(10, len);
      var intNum = parseInt(floatNum * times + 0.5, 10);
      ret.times = times;
      ret.num = intNum;
      return ret
  }

/*
   * 核心方法，实现加减乘除运算，确保不丢失精度
   * 思路：把小数放大为整数（乘），进行算术运算，再缩小为小数（除）
   *
   * @param a {number} 运算数1
   * @param b {number} 运算数2
   * @param op {string} 运算类型，有加减乘除（add/subtract/multiply/divide）
   *
   */
  function operation(a, b, op) {
      var o1 = toInteger(a);
      var o2 = toInteger(b);
      var n1 = o1.num;
      var n2 = o2.num;
      var t1 = o1.times;
      var t2 = o2.times;
      var max = t1 > t2 ? t1 : t2;
      var result = null;
      switch (op) {
          case 'add':
              if (t1 === t2) { // 两个小数位数相同
                  result = n1 + n2
              } else if (t1 > t2) { // o1 小数位 大于 o2
                  result = n1 + n2 * (t1 / t2)
              } else { // o1 小数位 小于 o2
                  result = n1 * (t2 / t1) + n2
              }
              return result / max;
          case 'subtract':
              if (t1 === t2) {
                  result = n1 - n2
              } else if (t1 > t2) {
                  result = n1 - n2 * (t1 / t2)
              } else {
                  result = n1 * (t2 / t1) - n2
              }
              return result / max;
          case 'multiply':
              result = (n1 * n2) / (t1 * t2);
              return result;
          case 'divide':
              result = (n1 / n2) * (t2 / t1);
              return result
      }
  }

  // 加减乘除的四个接口
  function add(a, b) {
      return operation(a, b, 'add')
  }

  function subtract(a, b) {
      return operation(a, b, 'subtract')
  }

  function multiply(a, b) {
      return operation(a, b, 'multiply')
  }

  function divide(a, b) {
      return operation(a, b, 'divide')
  }

  // exports
  return {
      add: add,
      subtract: subtract,
      multiply: multiply,
      divide: divide
  }
}();
function checkform(){
    if(parseFloat($("#allmoney").val())<=0){
        alert('输入数量错误！');return false;
    }
    $.post("{fun U('user/buy')}",{allmoney:$("#allmoney").val(),paytype:$("#paytype").val(),number:$("#money").val(),buytarget:$("#buytarget").val()},function(res){
      console.log(res);
      if(res.code==0){
          var url = "{fun U('order/pay')}?go=1&orderno="+res['data'].orderno+"&paytype="+res['data'].paytype;
          window.location.href = url;
      }else{
        alert(res.msg);
      }

    },'json');

    return false;

}
function count_money(){
    var money_exchange = 1/parseFloat({$webconf['money_exchange']});
    var jifen_exchange = 1/parseFloat({$webconf['jifen_exchange']});
    if(parseInt($("#buytarget").val())==1){
        var b = money_exchange;
    }else{
        var b = jifen_exchange;
    }
    var a = parseInt($("#money").val());
    if(a<0){
      alert('输入数量错误！');$("#paymoney").html(0);return false;
    }
    if(a==0){
      $("#paymoney").html(0);return false;
    }
    var m = floatObj.multiply(a,b);
    $("#allmoney").val(m);
    $("#paymoney").html(m);
}


</script>
</body>
</html>